#include "StdAfx.h"

// Var-Int encoding
// An encoding scheme for unsigned integers
// aims to use less memory/bandwidth for smaller numbers than larger numbers

// already have:
// reads numBitsToSend out of content, and sends it over the network

//===========================================================================
void Output(
    unsigned int  content,
    unsigned char numBitsToSend
) {
    // do something
}

// in binary: 
// if someone calls Encode( 0000 [...] 0000 0111 0010 1010)
// encode should call:
// Output(1010, 4) // write lowest four bits to stream
// Output(1, 1) // metadata: There is more
// Output(0010, 4) // write the next lowest four bits to stream
// output(1, 1) // metadat: there is more
// Output(0111, 4) // write the next lowest four bits to the stream
// Output(0, 1) // metadata: the rest of the bits are all zero

// Encode(0) => Output(0000, 4); Output(0, 1);

// Encode (0101 0000) => Output(0000, 4); Output(1,1); Output(0101, 4); Output(0,1);

//===========================================================================
void Encode(
    unsigned int value
) {
    const unsigned char count = 4;
    
    int lowest = 0;
    while( true ) {
        lowest = value >> count;
        lowest = value - lowest;
        Output( lowest, count );
        
        value = value >> count;
        if( !value )
            break;
            
        Output( 1, 1 );
    }
    Output( 0, 1 );
}